
/**   
*    
* 项目名称：a_yongan   
* 类名称：PersonalWorkServiceImpl   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年8月27日 下午8:58:30    
* 修改时间：2018年8月27日 下午8:58:30   
* 修改备注：   
* @version    
*    
*/
package com.yongan.workflow.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yongan.user.dao.UserDao;
import com.yongan.user.entity.User;
import com.yongan.user.exception.UserException;
import com.yongan.workflow.dao.FlowDetailsDao;
import com.yongan.workflow.dao.PersonalWorkDao;
import com.yongan.workflow.entity.FlowDetails;
import com.yongan.workflow.entity.FlowInfo;
import com.yongan.workflow.entity.PersonalWork;
import com.yongan.workflow.service.FlowInfoService;
import com.yongan.workflow.service.PersonalWorkService;

/**
 * @author liubo
 *
 */
@Service
public class PersonalWorkServiceImpl implements PersonalWorkService {

	@Autowired
	private PersonalWorkDao personalWorkDao;
	@Autowired
	private FlowInfoService flowInfoService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private FlowDetailsDao flowDetailsDao;
	@Override
	public List<PersonalWork> getPersonalWorkList(int offset, int limit,
			String userId) {
		return personalWorkDao.queryAll(offset, limit, userId);
	}

	
	@Override
	public List<PersonalWork> getPersonalWorkListAdmin(int offset, int limit) {
		return personalWorkDao.queryAllAdmin(offset, limit);
	}

	
	@Override
	public PersonalWork getPersonalWorkById(String personalWorkId) {
		return personalWorkDao.queryById(personalWorkId);
	}

	
	@Override
	public Boolean addPersonalWork(String url,String userId,String docId,String type) throws UserException {
		FlowInfo flowInfo = flowInfoService.getFlowInfoByUrl(url,type);
		User user = userDao.queryById(userId);
		PersonalWork personalWork = new PersonalWork();
		System.out.println(docId+"111111111111111111111");
		personalWork.setPersonalWorkDocId(docId);
		personalWork.setPersonalWorkId(UUID.randomUUID().toString());
		personalWork.setPersonalWorkDb(flowInfo.getUseDb());
		personalWork.setPersonalWorkDbUrl(flowInfo.getUseDbUrl());
		List<FlowDetails> details = flowInfo.getDetails();
		for(FlowDetails detail:details){
			//System.out.println("========"+detail.getFlowDetailsUp()+detail.getFlowDetailsNumber());
			if(detail.getFlowDetailsUp().equals("")&detail.getFlowDetailsNumber().equals("001")){
				personalWork.setPersonalWorkFlowId(detail.getFlowDetailsId());
				personalWork.setPersonalWorkFlowName(detail.getFlowDetailsName());
			}
		}
		personalWork.setPersonalWorkIsEnd("0");
		personalWork.setPersonalWorkTitle("来自用户"+user.getUserName()+"的"+flowInfo.getUseDb());
		personalWork.setPersonalWorkUser(user.getUserName());
		personalWork.setPersonalWorkUserId(user.getUserId());
		personalWork.setPersonalWorkUsers(user.getUserName());
		personalWork.setPersonalWorkUsersId(user.getUserId());
		personalWork.setPersonalWorkIsReturn("false");
		int rs = personalWorkDao.insertPersonalWork(personalWork);
		if(rs<=0){
			//return false;
			throw new UserException("add personalwork failed");
		}else{
			return true;
		}
	}

	
	@Override
	public Boolean deletePersonalWork(String docId) throws UserException {
		int deleteCount = personalWorkDao.deletePersonalWorkByDocId(docId);
		if(deleteCount<=0){
			throw new UserException("delete personalwork failed");
		}else{
			return true;
		}
	}

	
	@Override
	public Boolean nextPersonalWork(String docId,String type) throws UserException {
		PersonalWork pw = personalWorkDao.queryByDocId(docId);
		FlowInfo fi = flowInfoService.getFlowInfoByUrl(pw.getPersonalWorkDbUrl(),type);
		
		pw.setPersonalWorkUpUser(pw.getPersonalWorkUser());
		pw.setPersonalWorkUpUserId(pw.getPersonalWorkUserId());
		FlowDetails fd = flowDetailsDao.queryById(pw.getPersonalWorkFlowId());
		System.out.println(fd.getFlowDetailsDownId()+"====="+fi.getFlowInfoId());
		FlowDetails fdnext = flowDetailsDao.queryByNumber(fd.getFlowDetailsDownId(), fi.getFlowInfoId());
		pw.setPersonalWorkFlowId(fdnext.getFlowDetailsId());
		pw.setPersonalWorkFlowName(fdnext.getFlowDetailsName());
		pw.setPersonalWorkUser(fdnext.getFlowDetailsFzr());
		pw.setPersonalWorkUserId(fdnext.getFlowDetailsFzrId());
		pw.setPersonalWorkIsReturn("false");
		String allUsers = pw.getPersonalWorkUsers();
		pw.setPersonalWorkUsers(allUsers+";"+fdnext.getFlowDetailsFzr());
		String allUsersId = pw.getPersonalWorkUsersId();
		pw.setPersonalWorkUsersId(allUsersId+";"+fdnext.getFlowDetailsFzrId());
		
		int rs = personalWorkDao.updatePersonalWork(pw);
		if(rs<=0){
			throw new UserException("add personalwork failed");
		}else{
			return true;
		}
	}

	
	@Override
	public Boolean upPersonalWork(String docId,String type) throws UserException {
		PersonalWork pw = personalWorkDao.queryByDocId(docId);
		FlowInfo fi = flowInfoService.getFlowInfoByUrl(pw.getPersonalWorkDbUrl(),type);
		pw.setPersonalWorkUser(pw.getPersonalWorkUpUser());
		pw.setPersonalWorkUserId(pw.getPersonalWorkUpUserId());
		pw.setPersonalWorkIsReturn("true");
		FlowDetails fd = flowDetailsDao.queryById(pw.getPersonalWorkFlowId());
		FlowDetails fdup = flowDetailsDao.queryByNumber(fd.getFlowDetailsUpId(), fi.getFlowInfoId());
		pw.setPersonalWorkFlowId(fdup.getFlowDetailsId());
		pw.setPersonalWorkFlowName(fdup.getFlowDetailsName());
		int rs = personalWorkDao.updatePersonalWork(pw);
		if(rs<=0){
			throw new UserException("add personalwork failed");
		}else{
			return true;
		}
	}

	
	@Override
	public int getCount(String userId) {
		return personalWorkDao.queryCount(userId);
	}

	@Override
	public int getCountAdmin() {
		return personalWorkDao.queryCountAdmin();
	}


	/* （非 Javadoc）
	 * @see com.yongan.workflow.service.PersonalWorkService#getFlowDetail(java.lang.String)
	 */
	@Override
	public FlowDetails getFlowDetail(String docId) {
		PersonalWork pw = personalWorkDao.queryByDocId(docId);
		FlowDetails fd = flowDetailsDao.queryById(pw.getPersonalWorkFlowId());
		return fd;
	}


	/* （非 Javadoc）
	 * @see com.yongan.workflow.service.PersonalWorkService#getPersonalWorkListUp(int, int, java.lang.String)
	 */
	@Override
	public List<PersonalWork> getPersonalWorkListUp(int offset, int limit,
			String userId) {
		String userIdd = "%"+userId+"%";
		return personalWorkDao.queryAllUp(offset, limit, userId,userIdd);
	}


	/* （非 Javadoc）
	 * @see com.yongan.workflow.service.PersonalWorkService#getCountYiBan(java.lang.String)
	 */
	@Override
	public int getCountYiBan(String userId) {
		String userIdd = "%"+userId+"%";
		return personalWorkDao.queryCountYiBan(userId, userIdd);
	}


	/* （非 Javadoc）
	 * @see com.yongan.workflow.service.PersonalWorkService#getPersonalWork(java.lang.String)
	 */
	@Override
	public PersonalWork getPersonalWork(String docId) {
		PersonalWork pw = personalWorkDao.queryByDocId(docId);
		return pw;
	}
	
	
	

}
